/**
 * Mock数据统一管理模块
 * 用于确保所有模块之间的数据逻辑一致性
 */
import bloodSugarRecords from './bloodSugarRecords.js'
import bloodSugarStatistics from './bloodSugarStatistics.js'
import reminders from './reminders.js'
import communityPosts from './communityPosts.js'
import communityComments from './communityComments.js'
import articles from './articles.js'

// 当前用户信息
export const currentUser = {
  id: 1,
  nickname: '糖糖',
  avatar: '/static/images/placeholder.png',
  gender: 1, // 1男 2女
  age: 45,
  diabetesType: 2, // 1型、2型
  diagnosisDate: '2020-05-15',
  healthScore: 85,
  height: 174, // cm
  weight: 68,  // kg
  bmi: 22.5,
  targetBloodSugar: {
    fasting: { min: 3.9, max: 5.6 },
    afterMeal: { min: 3.9, max: 7.8 },
    beforeSleep: { min: 3.9, max: 6.7 }
  }
}

// 当前日期时间
export const getCurrentDate = () => {
  const now = new Date()
  const year = now.getFullYear()
  const month = (now.getMonth() + 1).toString().padStart(2, '0')
  const day = now.getDate().toString().padStart(2, '0')
  return `${year}-${month}-${day}`
}

export const getCurrentTime = () => {
  const now = new Date()
  const hours = now.getHours().toString().padStart(2, '0')
  const minutes = now.getMinutes().toString().padStart(2, '0')
  return `${hours}:${minutes}`
}

export const getCurrentDateTime = () => {
  return `${getCurrentDate()} ${getCurrentTime()}`
}

// 获取今日血糖记录
export const getTodayBloodSugarRecords = () => {
  const today = getCurrentDate()
  return bloodSugarRecords.filter(record => record.date === today)
}

// 获取最新的血糖记录
export const getLatestBloodSugarRecord = () => {
  const sortedRecords = [...bloodSugarRecords].sort((a, b) => {
    return new Date(b.datetime) - new Date(a.datetime)
  })
  return sortedRecords[0] || null
}

// 获取血糖状态评估（正常、偏高、偏低）
export const getBloodSugarStatus = (value, type) => {
  let target = currentUser.targetBloodSugar.afterMeal
  
  if (type.includes('before') && !type.includes('Sleep')) {
    target = currentUser.targetBloodSugar.fasting
  } else if (type.includes('beforeSleep')) {
    target = currentUser.targetBloodSugar.beforeSleep
  }
  
  if (value < target.min) {
    return 'low'
  } else if (value > target.max) {
    return 'high'
  } else {
    return 'normal'
  }
}

// 获取今日待完成的提醒
export const getTodayReminders = () => {
  // 获取今天是周几（0-6，0代表周日）
  const today = new Date().getDay()
  // 转换为1-7，表示周一至周日
  const weekday = today === 0 ? 7 : today
  
  return reminders.filter(reminder => {
    if (!reminder.active) return false
    
    if (reminder.repeat === 'daily') {
      return true
    } else if (reminder.repeat === 'weekly') {
      return reminder.repeatDays.includes(weekday)
    } else if (reminder.repeat === 'once') {
      // 一次性提醒，判断日期是否是今天
      const reminderDate = reminder.date
      return reminderDate === getCurrentDate()
    }
    return false
  })
}

// 获取已经完成的提醒数量
export const getCompletedRemindersCount = () => {
  // 这里应该从本地存储获取已完成的提醒，此处仅为模拟
  return Math.floor(getTodayReminders().length * 0.6)
}

// 获取饮食记录
export const getDietRecords = (date = getCurrentDate()) => {
  // 模拟饮食记录数据
  const breakfastCalories = 450
  const lunchCalories = 650
  const dinnerCalories = 570
  const snackCalories = 180
  
  return {
    breakfast: {
      time: '07:30',
      foods: [
        { name: '全麦面包', amount: '2片', calories: 160, carbs: 30, protein: 6, fat: 2 },
        { name: '鸡蛋', amount: '1个', calories: 80, carbs: 1, protein: 6, fat: 5 },
        { name: '牛奶', amount: '250ml', calories: 150, carbs: 12, protein: 8, fat: 8 },
        { name: '香蕉', amount: '1根', calories: 60, carbs: 15, protein: 1, fat: 0 }
      ],
      total: {
        calories: breakfastCalories,
        carbs: 58,
        protein: 21,
        fat: 15
      }
    },
    lunch: {
      time: '12:00',
      foods: [
        { name: '米饭', amount: '1小碗', calories: 200, carbs: 44, protein: 4, fat: 0.5 },
        { name: '清蒸鱼', amount: '100g', calories: 120, carbs: 0, protein: 22, fat: 3 },
        { name: '炒青菜', amount: '1份', calories: 60, carbs: 6, protein: 3, fat: 3 },
        { name: '番茄蛋汤', amount: '1碗', calories: 100, carbs: 5, protein: 6, fat: 6 },
        { name: '苹果', amount: '1个', calories: 70, carbs: 18, protein: 0.5, fat: 0.5 }
      ],
      total: {
        calories: lunchCalories,
        carbs: 73,
        protein: 35.5,
        fat: 13
      }
    },
    dinner: {
      time: '18:30',
      foods: [
        { name: '燕麦粥', amount: '1碗', calories: 150, carbs: 27, protein: 5, fat: 2.5 },
        { name: '蒸鸡胸肉', amount: '100g', calories: 165, carbs: 0, protein: 31, fat: 3.5 },
        { name: '凉拌黄瓜', amount: '1份', calories: 30, carbs: 6, protein: 1, fat: 0 },
        { name: '紫薯', amount: '1个', calories: 130, carbs: 30, protein: 1, fat: 0.1 },
        { name: '西红柿', amount: '1个', calories: 35, carbs: 8, protein: 1, fat: 0.4 }
      ],
      total: {
        calories: dinnerCalories,
        carbs: 71,
        protein: 39,
        fat: 6.5
      }
    },
    snack: {
      time: '15:30',
      foods: [
        { name: '酸奶', amount: '200g', calories: 100, carbs: 14, protein: 5, fat: 2.5 },
        { name: '坚果', amount: '15g', calories: 80, carbs: 2, protein: 3, fat: 7 },
      ],
      total: {
        calories: snackCalories,
        carbs: 16,
        protein: 8,
        fat: 9.5
      }
    },
    total: {
      calories: breakfastCalories + lunchCalories + dinnerCalories + snackCalories,
      carbs: 218,
      protein: 103.5,
      fat: 44
    }
  }
}

// 获取健身活动记录
export const getExerciseRecords = (date = getCurrentDate()) => {
  return [
    {
      id: 1,
      type: 'walking',
      typeText: '健走',
      startTime: '06:30',
      endTime: '07:00',
      duration: 30, // 分钟
      calories: 120,
      steps: 3600,
      distance: 2.7, // 公里
      heartRate: { min: 75, max: 95, avg: 85 }
    },
    {
      id: 2,
      type: 'taichi',
      typeText: '太极',
      startTime: '19:00',
      endTime: '19:20',
      duration: 20, // 分钟
      calories: 80,
      heartRate: { min: 65, max: 85, avg: 75 }
    }
  ]
}

// 数据导出
export default {
  bloodSugarRecords,
  bloodSugarStatistics,
  reminders,
  communityPosts,
  communityComments,
  articles,
  currentUser,
  getCurrentDate,
  getCurrentTime,
  getCurrentDateTime,
  getTodayBloodSugarRecords,
  getLatestBloodSugarRecord,
  getBloodSugarStatus,
  getTodayReminders,
  getCompletedRemindersCount,
  getDietRecords,
  getExerciseRecords
} 